package com.yc.javaee.tomcat.impl;

import com.yc.javaee.tomcat.Cookie;

public class CookieImpl implements Cookie {

    private String name;
    private String value;
    private String domain;
    private String path;
    private Integer maxAge;

    public CookieImpl(String name, String value) {
        this.name = name;
        this.value = value;
    }

    // Set-Cookie: UserID=JohnDoe; Max-Age=3600; Version=1
    @Override
    public String toString() {
        String ret = "%s=%s";
        ret = String.format(ret, name, value);
        if (domain != null){
            ret += "; domain=" + domain;
        }
        if (path != null){
            ret += "; path=" + path;
        }
        if (maxAge != null){
            ret += "; Max-Age=" + maxAge;
        }
        return ret;
    }

    @Override
    public void setDomain(String pattern) {
        this.domain = pattern;
    }

    @Override
    public void setMaxAge(int expiry) {
        this.maxAge = expiry;
    }

    @Override
    public void setPath(String uri) {
        this.path = uri;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getValue() {
        return value;
    }
}
